import java.security.Security;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ColumnListHandler;

import examples.mail.Db;

public class CopyOfSmtpSendersendtoall {
	QueryRunner qr = null;
	private Connection conn = null;
	private PreparedStatement ps = null;
	private List<Object> atts = null;

	public CopyOfSmtpSendersendtoall() throws Exception {
		qr = new QueryRunner(Db.getDataSource());
		// conn = Db.getDataSource().getConnection();
		// conn.setAutoCommit(false);
		// ps =
		// conn.prepareStatement("update acc_csdn set stat=?  where too=? ");

		String sql1 = "select   att    from acc_csdn     where stat='1'    group   by  att  order by    count(1)      ";
		atts = qr.query(sql1, new ColumnListHandler("att"));
	}

	public void run() throws Exception {
		for (Object att : atts) {
			String at = (String) att;
			String sql = "select too from acc_csdn where stat='1'  and  test='0'  and   att = ?  limit 1  ";
			List<Object> tos = qr.query(sql, new ColumnListHandler("too"), at);

			String[] toos = tos.toArray(new String[1]);

			String mailServerHost = "smtp.gmail.com";

			String mailServerPort = "465";
			String username = "nml40241@gmail.com";
			String password = "Wen101570474";
			String sender = "大唐世家<nml40241@gmail.com>";
			String subject = "百度一下你就知道";
			String content = "百度一下你就知道    <a href=\"http://126.am/yibbJ3\">百度一下你就知道</a>";
			boolean ssl = true;

			this.send(mailServerHost, mailServerPort, username, password, sender, toos, subject, content, ssl);

			Thread.sleep(1000);

			System.out.println(at);
		}
	}

	public void send(String mailServerHost, String mailServerPort, String username, String password, String sender, String[] tos, String subject,
			String content, boolean ssl) throws Exception {

		MyAuthenticator authenticator = null;

		Properties pro = new Properties();

		if (ssl) {
			Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
			final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
			pro.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
			pro.setProperty("mail.smtp.socketFactory.fallback", "false");
			pro.setProperty("mail.smtp.socketFactory.port", mailServerPort );
		}

		pro.put("mail.smtp.host", mailServerHost);
		pro.put("mail.smtp.port", mailServerPort);
		pro.put("mail.smtp.auth", "true");

		authenticator = new MyAuthenticator(username, password);

		// 根据邮件会话属性和密码验证器构造一个发送邮件的session
		Session sendMailSession = Session.getDefaultInstance(pro, authenticator);

		// 根据session创建一个邮件消息
		Message mailMessage = new MimeMessage(sendMailSession);
		// 创建邮件发送者地址
		Address from = new InternetAddress(sender);
		// 设置邮件消息的发送者
		mailMessage.setFrom(from);
		// 创建邮件的接收者地址，并设置到邮件消息中
		for (String t : tos) {
			Address to = new InternetAddress(t);
			// Message.RecipientType.TO属性表示接收者的类型为TO
			mailMessage.addRecipient(Message.RecipientType.TO, to);
		}
		// 设置邮件消息的主题
		mailMessage.setSubject(subject);
		// 设置邮件消息发送的时间
		mailMessage.setSentDate(new Date());
		// MiniMultipart类是一个容器类，包含MimeBodyPart类型的对象
		Multipart mainPart = new MimeMultipart();
		// 创建一个包含HTML内容的MimeBodyPart
		BodyPart html = new MimeBodyPart();
		// 设置HTML内容
		html.setContent(content, "text/html; charset=utf-8");
		mainPart.addBodyPart(html);
		// 将MiniMultipart对象设置为邮件内容
		mailMessage.setContent(mainPart);
		// 发送邮件
		Transport.send(mailMessage);

	}

	public static void main(String[] args) throws Exception {
		String[] toos = { "nice2mu@qq.com" };

		String mailServerHost = "smtp.126.com";

		String mailServerPort = "25";
		String username = "nml186@126.com";
		String password = "38451662";
		String sender = "\"久久八一\" <nml186@126.com>";
		String subject = "百度一下你就知道";
		String content = "百度一下你就知道    <a href=\"http://126.am/yibbJ3\">百度一下你就知道</a>";
		boolean ssl = false;

		new CopyOfSmtpSendersendtoall()

		.send(mailServerHost, mailServerPort, username, password, sender, toos, subject, content, ssl);
	}

}
